package cn.eveo.refreshandloadmore.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import cn.eveo.refreshandloadmore.R;
import cn.eveo.refreshandloadmore.adapter.RecyclerAdapter;
import cn.eveo.refreshandloadmore.view.ERecyclerView;
import cn.eveo.refreshandloadmore.view.PtrOnRefreshListener;
import cn.eveo.refreshandloadmore.view.PtrWrapRecyclerView;


public class RecyclerFragment extends Fragment {

    private View emptyView;
    private PtrWrapRecyclerView tryLoadMoreLayout;
    private ERecyclerView recyclerView;
    private RecyclerAdapter adapter;
    private List<String> datas;

    private LinearLayoutManager linearLayoutManager;
    private GridLayoutManager gridLayoutManager;
    private StaggeredGridLayoutManager staggeredGridLayoutManager;

    private GridDividerLine itemDecoration;
    private StaggeredGridDividerLine staggeredGridDecoration;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_recycler, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        emptyView = getActivity().findViewById(R.id.empty);
        tryLoadMoreLayout = (PtrWrapRecyclerView) getActivity().findViewById(R.id.loadmore_framelayout);
        recyclerView = tryLoadMoreLayout.getRecyclerView();
        datas = new ArrayList<>();
        adapter = new RecyclerAdapter(getActivity(), datas);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        itemDecoration = new GridDividerLine(getContext());
        staggeredGridDecoration=new StaggeredGridDividerLine(getContext());

        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        recyclerView.addItemDecoration(staggeredGridDecoration);

        tryLoadMoreLayout.setLoadingMoreEnabled(true);
        tryLoadMoreLayout.setEmptyView(emptyView);

        isEmpty();
        tryLoadMoreLayout.setRefreshListener(new PtrOnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }

            @Override
            public void onLoadMore() {
                loadMoreData();
            }
        });

        Button button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tryLoadMoreLayout.autoRefresh();
            }
        });

    }

    private void isEmpty() {
        if (datas.size() <= 0) {
            emptyView.setVisibility(View.VISIBLE);
        } else {
            emptyView.setVisibility(View.GONE);
        }
    }

    private void refreshData() {

        tryLoadMoreLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                datas.clear();
                for (int i = 0; i < 10; i++) {
                    datas.add("data: " + i);
                }
                adapter.notifyDataSetChanged();
                tryLoadMoreLayout.onRefreshComplete();
                isEmpty();

            }
        }, 1000);
    }

    private void loadMoreData() {

        tryLoadMoreLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 8; i++) {
                    datas.add("data: " + i);
                }
                adapter.notifyDataSetChanged();
                tryLoadMoreLayout.onRefreshComplete();

                isEmpty();
            }
        }, 1000);
    }

}
